Re: [GENERAL] getting val of serial field after insert

Поиск
Список
Период
Сортировка
От Herouth Maoz
Тема Re: [GENERAL] getting val of serial field after insert
Дата
Msg-id l03130307b3ae6d29cc1d@[147.233.159.109]
обсуждение исходный текст
Ответ на Re: [GENERAL] getting val of serial field after insert  (postgres@taifun.interface-business.de)
Список pgsql-general
At 16:32 +0300 on 09/07/1999, postgres@taifun.interface-business.de wrote:


> The type SERIAL is internal represented as SEQUENCE and the
> name of that sequence is build temporary from the name of
> the relation, the name of the attribute and a trailing 'seq'.
> Use currval(), nextval() or setval() to query or modify the value.
> Or simple query
>
> SELECT last_value FROM distributors_id_seq;
>
> to obtain the last value of 'id'.

BEEEEEEP... Wrong, wrong, wrong!

The last_value in the sequence table may not be the value you just
inserted, because another user may have also added a row at the same time,
and got another number, and that will be the last one...

The proper way to get the actual number that *you* entered, is to use the
function currval( 'distributors_id_seq' ). This function returns the last
value issued from the sequence to your session.

We have discussed it several times before, either on the SQL list or here.
It would be a good idea to look in the list archives before asking a
question.

There really should be a FAQ item about this - this subject is raised again
and again. It is certainly frequently asked.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



В списке pgsql-general по дате отправления:

Предыдущее
От: Geraldo Lopes
Дата:
Сообщение: Re: [GENERAL] How to compile PosttgreSQL on NT
Следующее
От: Herouth Maoz
Дата:
Сообщение: Re: [GENERAL] rules failed